package servlet;

import java.io.*;
import java.sql.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import util.*;
import bll.*;
import model.*;
import dal.*;

/**
 * Servlet implementation class CompanyServlet
 */
@WebServlet(urlPatterns = { "/Phase", "/phase", "/PhaseServlet", "/phaseServlet" }, initParams = { @WebInitParam(name = "mode", value = "") })
public class PhaseServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * Default constructor.
	 */
	public PhaseServlet() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");

		String mode = request.getParameter("mode");

		if (mode.equalsIgnoreCase("new")) {

			if (RightUtil.<PhaseModel> checkInsertRight()) {

				// TODO : MORE CODE HERE

				getServletContext().getRequestDispatcher("/phaseNew.jsp").forward(request, response);
			} else {
				// TODO no right
			}
		} else if (mode.equalsIgnoreCase("edit")) {

			PhaseModel query = new PhaseModel();
			String id = request.getParameter("id");
			query.setId(Integer.parseInt(id));

			GenericDal<PhaseModel, PhaseModelExt> dal = new GenericDal<PhaseModel, PhaseModelExt>(new PhaseModel());

			PhaseModel obj = null;
			try {
				obj = dal.select(query);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			if (obj != null) {

				// TODO : MORE CODE HERE

				request.setAttribute("phase", obj);
				getServletContext().getRequestDispatcher("/phaseEdit.jsp").forward(request, response);

			} else {
				// TODO : data not found
			}
		} else if (mode.equalsIgnoreCase("view")) {

			PhaseModel query = new PhaseModel();
			String id = request.getParameter("id");
			query.setId(Integer.parseInt(id));

			GenericDal<PhaseModel, PhaseModelExt> dal = new GenericDal<PhaseModel, PhaseModelExt>(new PhaseModel());

			PhaseModel obj = null;
			try {
				obj = dal.select(query);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			if (obj != null) {
				// TODO : LOAD MASTER DATA HERE
				// It's specific code that cannot be generated

				request.setAttribute("phase", obj);
				getServletContext().getRequestDispatcher("/phaseView.jsp").forward(request, response);

			} else {
				// TODO : data not found
			}
		} else if (mode.equalsIgnoreCase("quickInsert")) {
			PhaseModel obj = new PhaseModel();

			// TODO set name here

			GenericDal<PhaseModel, PhaseModelExt> dal = new GenericDal<PhaseModel, PhaseModelExt>(new PhaseModel());

			int res = 0;
			try {
				res = dal.insert(obj);
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			// TODO : return value

		} else if (mode.equalsIgnoreCase("delete")) {

			PhaseModel query = new PhaseModel();
			String id = request.getParameter("id");
			query.setId(Integer.parseInt(id));

			GenericDal<PhaseModel, PhaseModelExt> dal = new GenericDal<PhaseModel, PhaseModelExt>(new PhaseModel());

			int res = Integer.MIN_VALUE;
			try {
				res = dal.delete(query);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			if (res == 0) {
				// TODO success
				response.sendRedirect("phaseList.jsp");
				//getServletContext().getRequestDispatcher("/phaseList.jsp").forward(request, response);
			} else {
				// TODO fail
			}
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");

		String mode = request.getParameter("mode");

		if (mode.equalsIgnoreCase("insert")) {

			PhaseModel obj = new PhaseModel();

			String name = request.getParameter("name");
			obj.setName(name);

			String code = request.getParameter("code");
			obj.setCode(code);

			String description = request.getParameter("description");
			obj.setDescription(description);

			String id_project = request.getParameter("id_project");
			if (id_project != null && !id_project.isEmpty()) {
				obj.setIdProject(Integer.parseInt(id_project));
			}

			String status = request.getParameter("status");
			if (status != null && !status.isEmpty()) {
				obj.setStatus(Integer.parseInt(status));
			}

			String plan_start_date = request.getParameter("plan_start_date");
//			String plan_start_date_time = request.getParameter("plan_start_date_time");
			if (plan_start_date != null && !plan_start_date.isEmpty()) {
				obj.setPlanStartDate(DateTimeUtil.parseDate(plan_start_date));
			}

			String plan_end_date = request.getParameter("plan_end_date");
//			String plan_end_date_time = request.getParameter("plan_end_date_time");
			if (plan_end_date != null && !plan_end_date.isEmpty()) {// && plan_end_date_time != null && !plan_end_date_time.isEmpty()) {
				obj.setPlanEndDate(DateTimeUtil.parseDate(plan_end_date));
			}

			String real_start_date = request.getParameter("real_start_date");
			//String real_start_date_time = request.getParameter("real_start_date_time");
			if (real_start_date != null && !real_start_date.isEmpty()) {
				obj.setRealStartDate(DateTimeUtil.parseDate(real_start_date));
			}

			String real_end_date = request.getParameter("real_end_date");
			//String real_end_date_time = request.getParameter("real_end_date_time");
			if (real_end_date != null && !real_end_date.isEmpty()) {// && real_end_date_time != null && !real_end_date_time.isEmpty()) {
				obj.setRealEndDate(DateTimeUtil.parseDate(real_end_date));
			}
			
			String remark = request.getParameter("remark");
			obj.setRemark(remark);

			String index = request.getParameter("index");
            if (index != null && !index.isEmpty()) {
			    obj.setIndex(Integer.parseInt(index));
            }

			obj.setCreator(SessionUtil.getCurrentUserID());

			if (CommonBll.checkInputs(obj)) {
				GenericDal<PhaseModel, PhaseModelExt> dal = new GenericDal<PhaseModel, PhaseModelExt>(new PhaseModel());

				int res = 0;
				try {
					res = dal.insert(obj);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				if (res > 0) {
					// TODO : success, redirect to somewhere
					response.sendRedirect("phaseNew.jsp?idProject=" + obj.getIdProjectToString());
					//getServletContext().getRequestDispatcher("/phaseNew.jsp").forward(request, response);
				} else {
					// TODO : processing error here
				}
			} else {

				// invalid input

				request.setAttribute("phase", obj);
				getServletContext().getRequestDispatcher("/phaseInput.jsp").forward(request, response);

			}
		} else if (mode.equalsIgnoreCase("update")) {

			PhaseModel query = new PhaseModel();
			String id = request.getParameter("id");
			query.setId(Integer.parseInt(id));

			GenericDal<PhaseModel, PhaseModelExt> dal = new GenericDal<PhaseModel, PhaseModelExt>(new PhaseModel());

			PhaseModel obj = null;
			try {
				obj = dal.select(query);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			if (obj != null) {

				String name = request.getParameter("name");
				obj.setName(name);

//				String code = request.getParameter("code");
//				obj.setCode(code);

				String description = request.getParameter("description");
				obj.setDescription(description);

				String id_project = request.getParameter("id_project");
				if (id_project != null && !id_project.isEmpty()) {
					obj.setIdProject(Integer.parseInt(id_project));
				}

				String status = request.getParameter("status");
				if (status != null && !status.isEmpty()) {
					obj.setStatus(Integer.parseInt(status));
				}


				String plan_start_date = request.getParameter("plan_start_date");
//				String plan_start_date_time = request.getParameter("plan_start_date_time");
				if (plan_start_date != null && !plan_start_date.isEmpty()) {
					obj.setPlanStartDate(DateTimeUtil.parseDate(plan_start_date));
				}

				String plan_end_date = request.getParameter("plan_end_date");
//				String plan_end_date_time = request.getParameter("plan_end_date_time");
				if (plan_end_date != null && !plan_end_date.isEmpty()) {
					obj.setPlanEndDate(DateTimeUtil.parseDate(plan_end_date));
				}

				String real_start_date = request.getParameter("real_start_date");
				if (real_start_date != null && !real_start_date.isEmpty()) {
					obj.setRealStartDate(DateTimeUtil.parseDate(real_start_date));
				}

				String real_end_date = request.getParameter("real_end_date");
				if (real_end_date != null && !real_end_date.isEmpty()) {
					obj.setRealEndDate(DateTimeUtil.parseDate(real_end_date));
				}
				
				String remark = request.getParameter("remark");
				obj.setRemark(remark);

				String index = request.getParameter("index");
                if (index != null && !index.isEmpty()) {
				    obj.setIndex(Integer.parseInt(index));
                }
				if (CommonBll.checkInputs(obj)) {

					int res = Integer.MIN_VALUE;
					try {
						res = dal.update(obj);
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (ClassNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

					if (res == 0) {
						// TODO success
						response.sendRedirect("phaseNew.jsp?idProject=" + obj.getIdProject());
						//getServletContext().getRequestDispatcher("/phaseNew.jsp").forward(request, response);
					} else {
						// TODO error
					}
				} else {
					request.setAttribute("phase", obj);
					getServletContext().getRequestDispatcher("/phaseEdit.jsp").forward(request, response);
				}
			} else {
				// TODO data not found
			}
		} else if (mode.equalsIgnoreCase("delete")) {

			PhaseModel query = new PhaseModel();
			String id = request.getParameter("id");
			query.setId(Integer.parseInt(id));

			GenericDal<PhaseModel, PhaseModelExt> dal = new GenericDal<PhaseModel, PhaseModelExt>(new PhaseModel());

			int res = Integer.MIN_VALUE;
			try {
				res = dal.delete(query);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			if (res == 0) {
				// TODO success
				response.sendRedirect("phaseList.jsp");
				//getServletContext().getRequestDispatcher("/phaseList.jsp").forward(request, response);
			} else {
				// TODO fail
			}
		} else if (mode.equalsIgnoreCase("quickInsert")) {
			PhaseModel obj = new PhaseModel();

			// TODO set name here

			GenericDal<PhaseModel, PhaseModelExt> dal = new GenericDal<PhaseModel, PhaseModelExt>(new PhaseModel());

			int res = 0;
			try {
				res = dal.insert(obj);
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			// TODO : return value

		}
	}

}